package com.into.common.csv.converters;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.into.common.csv.CSVConverter;

/**
 * Converts a Date as specified in {@link #dateFormat}
 * 
 * @author Zied Hamdi
 * 
 */
public class DateConverter implements CSVConverter<Date> {
	protected DateFormat	dateFormat;

	public DateConverter(DateFormat dateFormat) {
		this.dateFormat = dateFormat;
	}

	public DateConverter(String dateFormat) {
		this.dateFormat = new SimpleDateFormat(dateFormat);
	}

	public DateFormat getDateFormat() {
		return dateFormat;
	}

	public void setDateFormat(DateFormat dateFormat) {
		this.dateFormat = dateFormat;
	}

	@Override
	public Date fromString(String value) throws ParseException {
		return dateFormat.parse(value);
	}

	@Override
	public String toString(Date value) {
		if (value != null)
			return dateFormat.format(value);
		else
			return "";

	}

}
